const moment = require('../../utils/moment')
let app = getApp()
// 获取云数据库引用
const db = wx.cloud.database()
const _ = db.command
const admin = db.collection('adminlist')
let signUp = []
const thisMsg = wx.getStorageSync('loginMsg') && JSON.parse(wx.getStorageSync('loginMsg'))

Page({

  /**
   * 页面的初始数据
   */
  data: {
    _id: '',
    signUp: [],
    hour: moment().hour(),
    loading: false,
    tabbar: {}
  },

  // 获取打卡数组
  huoqudaka () {
    let _id = thisMsg._id
    if (_id !== undefined) {
      admin.doc(_id).get({
        success (res) {
          signUp = res.data.signUp
          _id = res.data._id
          this.setData({
            signUp,
            _id
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    app.editTabbar()
  },

  /**
   * 监听页面显示
   */
  onShow () {
    this.huoqudaka()
  },

  // 判断这一天是否已经签到
  panduanqiandao () {
    let flag = true
    const today = parseInt(moment().format('YYYYMMDD'))
    this.huoqudaka()
    let signs = signUp
    if (signs !== '') {
      for (let i = 0; i < signs.length; i++) {
        if (signs[i] === today) {
          flag = false
          break
        }
      }
    }
    if (thisMsg.signUp !== '') {
      let signsNew = thisMsg.signUp && thisMsg.signUp[thisMsg.signUp.length - 1]
      if (signsNew === today) {
        flag = false
      }
    }
    return flag
  },

  // 判断时间点
  panduanshijian () {
    let flag = true
    if (this.data.hour <= 23 && this.data.hour >= 20) {
    // if (this.data.hour <= 23 && this.data.hour >= 0) {
      flag = true
    } else {
      flag = false
    }
    return flag
  },

  // 打卡
  Daka () {
    if (this.loading) {
      return
    }
    this.setData({ loading: true })
    const loginStatu = app.globalData.isLogin()
    if (!loginStatu) {
      wx.showToast({
        title: '未登录',
        icon: 'error',
        duration: 1500,
        mask: true
      })
      this.setData({ loading: false })
      return
    }
    let that = this
    let flag1 = that.panduanqiandao()
    let flag2 = that.panduanshijian()
    // 今日已打卡
    if (!flag1) {
      wx.hideTabBarRedDot({
        index: 0
      })
      wx.reLaunch({
        url: '../index/index'
      })
      wx.showToast({
        title: '今日已打卡',
        icon: 'error',
        duration: 1500,
        mask: true
      })
      that.setData({ loading: false })
    // 不在下午八点到十二点之间
    } else if (!flag2) {
      wx.showToast({
        title: '不在时间段',
        icon: 'error',
        duration: 1500,
        mask: true
      })
      that.setData({ loading: false })
    } else {
      wx.getLocation({
        isHighAccuracy: true,
        highAccuracyExpireTime: 3000,
        success (res) {
          if (res.latitude >= 38.0143 && res.latitude <= 38.0145 &&
          res.longitude >= 112.4425 && res.longitude <= 112.4432) {
          // if (res.latitude >= 37 && res.latitude <= 39 &&
          //     res.longitude >= 112 && res.longitude <= 113) {
            let _id = thisMsg._id
            const today = parseInt(moment().format('YYYYMMDD'))
            admin.doc(_id).update({
              data: {
                signUp: _.push(today)
              }
            })
              .then((res) => {
                thisMsg.signUp.push(today)
                wx.setStorageSync('loginMsg', JSON.stringify(thisMsg))
                wx.reLaunch({
                  url: '../index/index'
                })
                wx.showToast({
                  title: '打卡成功',
                  icon: 'succes',
                  duration: 2000,
                  mask: true
                })
              })
          } else { // 不在指定地点附近
            wx.showToast({
              title: '你不在位置',
              icon: 'error',
              duration: 1500,
              mask: true
            })
          }
          that.setData({ loading: false })
        },
        fail () {
          wx.showToast({
            title: '获取位置失败',
            icon: 'error',
            duration: 1500,
            mask: true
          })
          that.setData({ loading: false })
        }
      })
    }
  },

  // 跳转排名界面
  toRanking () {
    wx.navigateTo({
      url: '../ranking/ranking'
    })
  }

})